package org.gd.hellocrypt;

import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class PrivatTest {

    private static String pass = "SdnNx4UdJSIP8je4bQfHX51n7VijmC3a";
    private static String key = pass;

    public static void main(String...a)
            throws NoSuchAlgorithmException, UnsupportedEncodingException {


        String data = "<oper>cmt</oper>";
        String encode = "UTF-8";







        String s1 = "38095c058bced1a20d8fb7931cee19f257fc508";
        String s2 = LibUtil.sha1(LibUtil.md5(data + pass), encode);
        String s3 = LibUtil.toSHA1(LibUtil.md5(data + pass));

        System.out.println("signature s1: " + s1);
        System.out.println("signature s2: " + s2);
        System.out.println("signature s3: " + s3);

        s1 = "<oper>cmt</oper>" + key;
        s2 = LibUtil.sha1(LibUtil.md5(s1), encode);
        s3 = LibUtil.toSHA1(LibUtil.md5(s1));

        System.out.println("signature s1: " + s1);
        System.out.println("signature s2: " + s2);
        System.out.println("signature s3: " + s3);

        /* ================================================================= */

        String md5 = "fd42eaf7b61c2a829bf101cc0cb951ae";
        String sha1 = "0a47bd1b4a677216cb35ff715ca9303eaeb39d2b";

        System.out.println("signature md5: " + md5);
        System.out.println("signature md5: " + LibUtil.md5(pass));
        System.out.println("signature md5: " + md5.equals(LibUtil.md5(pass)));

        System.out.println("signature sha1: " + sha1);
        System.out.println("signature sha1: " + LibUtil.toSHA1(pass));
        System.out.println("signature sha1: " + sha1.equals(LibUtil.toSHA1(pass)));

    }

    public static String getHash(String text) throws Exception{
        return sha1(md5(text+key));
    }

    public static String md5(String string) throws Exception{
        MessageDigest m = MessageDigest.getInstance("MD5");
        m.reset();
        m.update(string.getBytes("UTF-8"));
        byte[] digest = m.digest();
        BigInteger bigInt = new BigInteger(1,digest);
        String hashtext = bigInt.toString(16);
        // Now we need to zero pad it if you actually want the full 32 chars.
        while(hashtext.length() < 32 ){
            hashtext = "0"+hashtext;
        }
        return hashtext;
    }

    public static String sha1(String string) throws Exception{
        MessageDigest m = MessageDigest.getInstance("SHA1");
        m.reset();
        m.update(string.getBytes("UTF-8"));
        byte[] digest = m.digest();
        BigInteger bigInt = new BigInteger(1,digest);
        String hashtext = bigInt.toString(16);
        // Now we need to zero pad it if you actually want the full 32 chars.
        while(hashtext.length() < 32 ){
            hashtext = "0"+hashtext;
        }
        return hashtext;
    }
}
